import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:ytbusiness/config/style.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';

///没有标题的AppBar
AppBar noTitleBar(BuildContext context, {String leftTxt = ""}) {
  return AppBar(
    elevation: 0,
    backgroundColor: Colors.white,
    leading: GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        Navigator.pop(context);
      },
      child: Row(
        children: [
          leftTxt == ""
              ? Container(
                  margin: EdgeInsets.only(left: setWidth(15)),
                  width: setWidth(10),
                  height: setWidth(17),
                  child: Image.asset(
                    "assets/images/return.png",
                    fit: BoxFit.cover,
                  ))
              : Container(
                  margin: EdgeInsets.only(left: setWidth(15)),
                  child: Text(
                    leftTxt,
                    style: TextStyle(
                        fontSize: setSp(14), color: Color(0xff37c2bb)),
                  )),
        ],
      ),
    ),
  );
}

///简单配置的文本组件   传入需要显示的文本即可，其他参数可选
Widget simpleText(String content,
    {double size = 15, Color color = YTStyle.textColor}) {
  return Text(
    content,
    style: TextStyle(
      color: color,
      fontSize: setSp(size),
    ),
    maxLines: 1,
    overflow: TextOverflow.ellipsis,
  );
}

///自定义标题的AppBar
AppBar customAppBar(BuildContext context,
    {String title = "", double elevation = 0}) {
  return AppBar(
    elevation: elevation,
    centerTitle: true,
    automaticallyImplyLeading: false,
    backgroundColor: Colors.white,
    title: Text(
      title,
      style: TextStyle(
          color: Color(0xff323232),
          fontSize: setSp(18),
          fontWeight: FontWeight.w600),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    ),
    leading: GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        Navigator.pop(context);
      },
      child: Row(
        children: [
          Container(
              margin: EdgeInsets.only(left: setWidth(15)),
              width: setWidth(10),
              height: setWidth(17),
              child: Image.asset(
                "assets/images/return.png",
                fit: BoxFit.cover,
              )),
        ],
      ),
    ),
  );
}

///静态评分组件
List<Widget> addStart(int count) {
  if (count > 5) {
    count = 5;
  }
  if (count < 0) {
    count = 0;
  }
  List<Widget> start = [];
  for (var i = 0; i < 5; i++) {
    if (i < count) {
      start.add(Image.asset(
        'assets/images/like_start.png',
        width: setWidth(13),
        height: setWidth(13),
        fit: BoxFit.cover,
      ));
    } else {
      start.add(Image.asset(
        'assets/images/unlike_start.png',
        width: setWidth(13),
        height: setWidth(13),
        fit: BoxFit.cover,
      ));
    }
  }
  return start;
}

///打电话
callPhone(String phone) async {
  String url = 'tel:' + phone;
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    toast("拨号失败");
  }
}

///订单列表
///title 店铺名 / state 订单状态 / orderNum 订单号 / accessories 配件数 / havePrice 已报价 / time 时间戳
Widget listItem({
  String title,
  String state,
  String orderNum,
  String accessories,
  String havePrice,
  String time,
}) {
  return Container(
    margin: EdgeInsets.only(
        top: setWidth(10), left: setWidth(10), right: setWidth(10)),
    padding: EdgeInsets.only(
        top: setWidth(20),
        bottom: setWidth(20),
        left: setWidth(15),
        right: setWidth(15)),
    decoration: BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(8),
    ),
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Container(
                  child: Text(
                    "$title",
                    style: TextStyle(
                        color: Color(0xff323232),
                        fontSize: setSp(16),
                        fontWeight: FontWeight.w600),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ),
              SizedBox(
                width: setWidth(10),
              ),
              Container(
                child: Text(
                  "$state",
                  style: TextStyle(color: YTTheme.theme, fontSize: setSp(14)),
                ),
              ),
            ],
          ),
        ),
        SizedBox(
          height: setWidth(12),
        ),
        Container(
          child: Text(
            "订单编号:$orderNum",
            style: TextStyle(color: Color(0xff989898), fontSize: setSp(14)),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        SizedBox(
          height: setWidth(26),
        ),
        Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Container(
                      child: Text(
                        "$accessories",
                        style: TextStyle(
                          color: Color(0xff1666F1),
                          fontSize: setSp(14),
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Container(
                      child: Text(
                        "$havePrice",
                        style: TextStyle(
                          color: Color(0xff4D90FF),
                          fontSize: setSp(11),
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                width: setWidth(10),
              ),
              Container(
                child: Text(
                  "$time",
                  style:
                      TextStyle(color: Color(0xff989898), fontSize: setSp(12)),
                ),
              ),
            ],
          ),
        ),
      ],
    ),
  );
}

///图标
Widget link({String image, String title, String phone}) {
  return GestureDetector(
    behavior: HitTestBehavior.opaque,
    onTap: () {
      callPhone(phone);
    },
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Container(
          width: setWidth(22),
          height: setWidth(22),
          child: Image.asset(
            "assets/images/$image",
            fit: BoxFit.cover,
          ),
        ),
        SizedBox(
          width: setWidth(3),
        ),
        Container(
          child: Text(
            title,
            style: TextStyle(
                color: Color(0xff323232),
                fontSize: setSp(14),
                fontWeight: FontWeight.w400),
          ),
        ),
      ],
    ),
  );
}

///汽配商 结算信息
Widget settlementInfo({String price, String serve, String sum}) {
  return Container(
    padding: EdgeInsets.only(top: setWidth(18), bottom: setWidth(18)),
    decoration: BoxDecoration(
        color: Colors.white, borderRadius: BorderRadius.circular(8)),
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
          child: Text("结算信息",
              style: TextStyle(
                  color: Color(0xff323232),
                  fontSize: setSp(16),
                  fontWeight: FontWeight.w600)),
        ),
        Container(
          margin: EdgeInsets.only(top: setWidth(13), bottom: setWidth(22)),
          height: setWidth(0.5),
          color: Color(0xffF0F0F0),
        ),
        Container(
          padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                child: Text("报价金额",
                    style: TextStyle(
                        color: Color(0xff989898), fontSize: setSp(16))),
              ),
              Container(
                child: Text("$price",
                    style: TextStyle(
                        color: Color(0xff323232),
                        fontSize: setSp(15),
                        fontWeight: FontWeight.w600)),
              ),
            ],
          ),
        ),
        SizedBox(
          height: setWidth(20),
        ),
        Container(
          padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                child: Text("订单服务费",
                    style: TextStyle(
                        color: Color(0xff989898), fontSize: setSp(16))),
              ),
              Container(
                child: Text("$serve",
                    style: TextStyle(
                        color: Color(0xff323232),
                        fontSize: setSp(15),
                        fontWeight: FontWeight.w600)),
              ),
            ],
          ),
        ),
        SizedBox(
          height: setWidth(38),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
              child: Text("结算金额：￥",
                  style:
                      TextStyle(color: Color(0xff323232), fontSize: setSp(15))),
            ),
            Container(
              padding: EdgeInsets.only(right: setWidth(15)),
              child: Text("$sum",
                  style: TextStyle(
                      color: Color(0xff323232),
                      fontSize: setSp(18),
                      fontWeight: FontWeight.bold)),
            ),
          ],
        ),
      ],
    ),
  );
}

///修理厂 结算信息
Widget autoMakersSettlement({String price, String serve, String sum}) {
  return Container(
    padding: EdgeInsets.only(top: setWidth(18), bottom: setWidth(18)),
    decoration: BoxDecoration(
        color: Colors.white, borderRadius: BorderRadius.circular(8)),
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
          child: Text("结算信息",
              style: TextStyle(
                  color: Color(0xff323232),
                  fontSize: setSp(16),
                  fontWeight: FontWeight.w600)),
        ),
        Container(
          margin: EdgeInsets.only(top: setWidth(13), bottom: setWidth(22)),
          height: setWidth(0.5),
          color: Color(0xffF0F0F0),
        ),
        Container(
          padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                child: Text("报价金额",
                    style: TextStyle(
                        color: Color(0xff323232), fontSize: setSp(14))),
              ),
              Container(
                  child: RichText(
                      text: TextSpan(children: [
                TextSpan(
                    text: "￥",
                    style: TextStyle(
                        color: Color(0xff323232),
                        fontSize: setSp(13),
                        fontWeight: FontWeight.bold)),
                TextSpan(
                    text: "$serve",
                    style: TextStyle(
                        color: Color(0xff323232),
                        fontSize: setSp(17),
                        fontWeight: FontWeight.bold))
              ]))),
            ],
          ),
        ),
        SizedBox(
          height: setWidth(20),
        ),
        Container(
          padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                child: Text("订单服务费",
                    style: TextStyle(
                        color: Color(0xff323232), fontSize: setSp(14))),
              ),
              Container(
                  child: RichText(
                      text: TextSpan(children: [
                TextSpan(
                    text: "￥",
                    style: TextStyle(
                        color: Color(0xff323232),
                        fontSize: setSp(13),
                        fontWeight: FontWeight.bold)),
                TextSpan(
                    text: "$serve",
                    style: TextStyle(
                        color: Color(0xff323232),
                        fontSize: setSp(17),
                        fontWeight: FontWeight.bold))
              ]))),
            ],
          ),
        ),
        SizedBox(
          height: setWidth(38),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Container(
              child: Text("总计:",
                  style:
                      TextStyle(color: Color(0xff323232), fontSize: setSp(15))),
            ),
            Container(
              child: Text("$sum",
                  style: TextStyle(
                      color: Color(0xffFF4D4D),
                      fontSize: setSp(18),
                      fontWeight: FontWeight.bold)),
            ),
            Container(
              padding: EdgeInsets.only(right: setWidth(15)),
              child: Text("元",
                  style:
                      TextStyle(color: Color(0xff323232), fontSize: setSp(15))),
            ),
          ],
        ),
      ],
    ),
  );
}

/// 没有内容
Widget emptyBox() {
  return Container(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          width: setWidth(51),
          height: setWidth(51),
          child: Image.asset(
            "assets/images/tool_car.png",
            fit: BoxFit.cover,
          ),
        ),
        Container(
            margin: EdgeInsets.only(top: setWidth(15)),
            child: Text(
              "暂无配件，请选择",
              style: TextStyle(
                color: Color(0xff333333),
                fontSize: setSp(12),
              ),
            ))
      ],
    ),
  );
}

/// 提交按钮
Widget confirmButton({String title, final VoidCallback voidCallback}) {
  return GestureDetector(
    onTap: () {
      voidCallback();
    },
    child: Container(
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
              top: BorderSide(color: Color(0xffDDDDDD), width: setWidth(0.5)))),
      child: Container(
        height: setWidth(45),
        alignment: Alignment.center,
        margin: EdgeInsets.only(
            left: setWidth(17),
            right: setWidth(17),
            top: setWidth(10),
            bottom: ScreenUtil.bottomBarHeight + 10),
        decoration: BoxDecoration(
            color: YTTheme.theme, borderRadius: BorderRadius.circular(3)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              title,
              style: TextStyle(color: Colors.white, fontSize: setSp(15)),
            ),
          ],
        ),
      ),
    ),
  );
}
